package net.betou.trade.role.setting;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import net.betou.core.entity.ConfigLogin;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Role;
import net.betou.trade.entity.main.RoleExt;

public class SuggestionFinderConfig extends CommonFinderConfig{
	
	private AgentAndAdmins admin;
	private RoleExt roleExt;
	protected Type type = Type.SELF;
	
	private String config;
	private Integer deep;

	public SuggestionFinderConfig(AgentAndAdmins admin) {
		super(admin, ConfigLogin.CONFIG_SUGGESTION);
		this.admin = admin;
		this.config = ConfigLogin.CONFIG_SUGGESTION;
		this.initRoleExts( );
	}
	
	@Override
	public Boolean isSelf() {
		return false;
	}
	
	@Override
	protected String getBegin( ) {
		return "bean.admin in(";
	}

	@Override
	protected String getEnd( ) {
		return ")";
	}
	
	/**
	 * 初始化 roleExts 
	 */
	protected void initRoleExts( ){
		Role role = admin.getSearchSettingRole( ); // 获取角色
		if( role == null ) {
			return;
		}
		
		Map< String, RoleExt > extMap = role.getSearchSettingRoleExt( );
		
		if( extMap == null ){
			return;
		}
		roleExt = extMap.get( config ); // 获取该配置
		
		this.deep = getDeep( );
	}
	
	private int getDeep( ){
		Operating op = operating( );
		Integer deep; // 如果deep为0,就表示还是自己的
		
		if( op == Operating.UPDATE ){ // 默认为findDeep
			deep = roleExt.getUpdateDeep( );
		}else if( op == Operating.DELETE ){
			deep = roleExt.getDeleteDeep( );
		}else {
			deep = roleExt.getFindDeep( );
		}
		
		if( deep == null ){
			deep = 0 ; // 进行一次空值的处理， 不过这种情况基本上不会出现
		}
		
		if( deep == 0 ){
			return deep; // 不进行判断，直接返回
		}
		
		String viewRoles = roleExt.getViewRoles( ); // 如果没有，只会为空
		if( StringUtils.isNotBlank( viewRoles ) && deep > 0){
			this.type = Type.FILTER;
		}
		
		return deep;
	}

	public String getDeepHql(  ){
		Integer adminId =  admin.getId( );
		String viewRoles = getViewRoles( );
		String hql = getHQL( deep, adminId, viewRoles );
		if(hql.length()>0){
			return getBegin( ) + this.getHQL( deep, adminId, viewRoles ) + getEnd( );
		}else{
			return "";
		}
	}
	
	private String getViewRoles( ){
		if(roleExt==null){
			return null;
		}
		String viewRoles = roleExt.getViewRoles( );
		
		String [ ] splitViewRoles = StringUtils.split( viewRoles, "," );
		
		if( splitViewRoles == null || splitViewRoles.length == 0 ){
			return null;
		}
		
		StringBuffer buffer = new StringBuffer( "(");
		
		for( int i = 0; i < splitViewRoles.length; i++ ){
			String id = splitViewRoles[ i ];
			
			buffer.append( "t.id=" );
			buffer.append( id );
			
			if( i < splitViewRoles.length - 1 ){
				buffer.append( " or " );
			}
		}
		
		buffer.append( ")" );
		
		return buffer.toString( );
	}
	
	private String getHQL( Integer deep, Integer adminId, String viewRoles ){
		StringBuffer hqlBuffer = new StringBuffer( );
		if(viewRoles!=null){
			hqlBuffer.append( "select a from AgentAndAdmins a join a.roles t" );
			hqlBuffer.append( " where a.deep <=" + deep );
			hqlBuffer.append( " and " + viewRoles );
			
			if( findSelf ( ) ){
				hqlBuffer.append( " or a.id=" + adminId );
			}
			
			hqlBuffer.append( " group by a.id" );
		}
		
		return hqlBuffer.toString( );
	}

}
